<template>
    <el-collapse v-model="activeNames">
        <el-collapse-item :title="item.title" :name="item.name" :key="item.name" v-for="item in data">
            <VueDraggable target=".el-row" v-model="item.children" :sort="false" :group="{name:'people',pull:'clone',put:false}"
             @start="onStart" :clone="clone" :setData="setData" @end="onEnd">
                <el-row>
                    <el-col :span="12" :key="col.name" v-for="col in item.children">
                        <div>
                            <img src="/public/lineLayout.png" loading="lazy" width="23">
                            <span class="title">{{ col.title }}</span>
                        </div>
                    </el-col>
                </el-row>
            </VueDraggable>
        </el-collapse-item>
    </el-collapse>
</template>
<script lang="ts" setup>
import { ref,inject } from 'vue'
import { ElCollapse, ElCollapseItem,ElRow,ElCol } from 'element-plus'
import { VueDraggable } from 'vue-draggable-plus'
import {compCategory as data,device} from '@drag/store'
import {menuCompData} from '@drag/componentData'
import {generateUUID} from '@dashboard/utils'
const activeNames = ref(['container'])
const system = [
    {
        title:'容器',
        name:'container',
        sort:0,
        children:[
            // {title:'页签组件',name:'tabComponent',sort:2,tabs:false}
            {title:'一栏布局',name:'columnarComponent',sort:1,tabs:false,props:{children:[
                {span:24,children:[]}
            ]}},
            {title:'二栏布局',name:'columnarComponent',sort:2,tabs:false,props:{children:[
                {span:12,children:[]},
                {span:12,children:[]}
            ]}},
            {title:'三栏布局',name:'columnarComponent',sort:3,tabs:false,props:{children:[
                {span:8,children:[]},
                {span:8,children:[]},
                {span:8,children:[]}
            ]}},
            {title:'自定义栏',name:'columnarComponent',sort:4,tabs:false,props:{children:[]}},
        ]
    },{
        title:'查询报表',
        name:'queryReport',
        sort:1,
        children:[
            {title:'查询组件',name:'query',tabs:false,sort:1},
            {title:'表格',name:'table',sort:2,tabs:true},
            {title:'表单',name:'form',sort:2,tabs:true},
        ]
    }
]
/** 仅保留组件所需的数据属性*/
const componentDataKeys = ['guid','title','name','tabs','style','props','config','data','defaultData']
if(device.value=='pc')data.value.push(...system)
//中央线通信
const eventBus = inject<any>('eventBus');
/**
 *  发拖动数据给手机渲染器
 * @param e 拖动开始事件对象
 */
const onStart = (e)=>{
    eventBus.emit('draggableData', e.clonedData);
}
const onEnd = (e)=>{
    eventBus.emit('dragEnd', true);
}
const setData = (dataTransfer:DataTransfer)=>{
    dataTransfer.setData('type', 'menu')
}
const clone = (element:menuCompData )=> {
    let newElement = JSON.parse(JSON.stringify(element));
    let guid = Date.now()
    if(newElement.name=='columnarComponent'){
        newElement.props!.children.forEach((e:any,i:any)=>{
            e.guid = guid+`${i}`;
            e.title = `${newElement.title}(栏${i+1})`
        })
    }
    else{
      newElement = {title:"页签",name:"tabs",guid:generateUUID(),data:[
        nitpick({...newElement,guid,config:{}})
      ]}
    }
    return newElement
}
/**
 * 精确检查并清理数据对象
 * 该函数旨在确保数据对象中只包含组件所需的数据属性
 * 它通过比较组件数据属性列表，移除多余的数据属性
 * 
 * @param {object} data - 需要检查和清理的数据对象
 * @returns {object} - 清理后的数据对象，仅包含组件所需的数据属性
 */
function nitpick(data:object){
    for (const key in data) {
        if(!componentDataKeys.includes(key))delete data[key];
    }
    return data
}
</script>
<style scoped>
img{
    padding: 0 2px 0 4px;
}
.title{
    font-size: 12px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.el-col{
    height: 40px;
    padding: 5px;
    box-sizing: border-box;
}
.el-col>div{
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    border:1px solid #eee;
    user-select: none;
    cursor: move;
}
.el-collapse-item>:deep(.el-collapse-item__header){
    background-color: #f2f5fc;
    height: 42px;
    padding-left: 20px;
}
.el-collapse-item>:deep(.el-collapse-item__wrap>.el-collapse-item__content){
    padding:8px;
}
</style>